package cc.mrbird.febs.music.dao;

import cc.mrbird.febs.music.domain.MusicBean;
import cc.mrbird.febs.music.domain.MusicToUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MusicMapper extends BaseMapper<MusicBean> {
    @Select("select * from (select t.id, t.song_name, t.singer, t.album, t.img, t.download_url from music t " +
            "where song_name like concat('%', concat(#{music.songName}, '%')) and " +
            "singer like concat('%', concat(#{music.singer}, '%'))) a left join " +
            "(select music_id ,mood_message, is_collect from music_mood_collect where user_id = #{music.musicMoodBean.userId}) b on a.id = b.music_id")
    /**
     * 查询音乐
     */
    IPage<MusicBean> getData(Page page, @Param("music") MusicBean music);

    /**
     * 查询收藏的音乐
     */
    @Select("select * from (select t.id, t.song_name, t.singer, t.album, t.img, t.download_url from music t " +
            "where song_name like concat('%', concat(#{music.songName}, '%')) and " +
            "singer like concat('%', concat(#{music.singer}, '%'))) a," +
            "(select music_id, mood_message, is_collect from music_mood_collect " +
            "where user_id = #{music.musicMoodBean.userId} and is_collect=1) b where a.id = b.music_id")
    IPage<MusicBean> getUserData(Page page, @Param("music") MusicBean music);

    /**
     * 喜欢该歌曲的用户
     */
    @Select("SELECT a.*, b.username FROM item_cf a left join t_user b on a.user_id = b.user_id where music_id = #{musicId}")
    List<MusicToUser> listUser(@Param("musicId") String musicId);

    @Select("select t.id, t.song_name, t.singer, t.album, t.img, t.download_url, r.rating from music t, user_cf r " +
            "where t.id = r.music_id and r.user_id = #{userId}")
    List<MusicBean> listMusicByUser(@Param("userId") String userId);
}
